using System;
using MySql.Data.MySqlClient;

namespace Scenario.Modules.DM.DataModel
{
	public class XMLDocumentInstance : XMLDocumentBase, bOS.DataBase.bOSTable 
	{
		private const string ID_ATTRIBUTE			= "Id";
		private const string IDMASTER_ATTRIBUTE		= "IdMaster";
		private const string VERSIONMASTER_ATTRIBUTE= "VersionMaster";
		private const string LASTDOCUMENT_ATTRIBUTE = "LastDocument";
	

		private string sId;
		private string sIdMaster;
		private string sVersionMaster;
		private string sLastDocument;

		private Scenario.WorkflowEngine.DataModel.WorkflowRelevantData.XMLAttributes wfAttributes ;
        private Scenario.WorkflowEngine.DataModel.WorkflowProcesses.XMLReports xmlReports;
        private Scenario.WorkflowEngine.DataModel.WorkflowProcesses.XMLLogs xmlLogs;
	
		private XMLDocumentVersions xmlDocumentVersions;

        public Scenario.WorkflowEngine.DataModel.WorkflowProcesses.XMLReports XMLReports
		{
			get { return xmlReports;}
			set { xmlReports = value;}
		}

        public Scenario.WorkflowEngine.DataModel.WorkflowProcesses.XMLLogs XMLLogs
		{
			get { return xmlLogs;}
			set { xmlLogs = value;}
		}

		public XMLDocumentInstance()
		{
			this.Init();
		}

		public XMLDocumentInstance(System.Xml.XmlNode node) : base(node)
		{
			this.Parse(node);
		}

		public XMLDocumentVersions XMLDocumentVersions
		{
			get { return xmlDocumentVersions;}
			set { xmlDocumentVersions = value;}
		}

		public string Id
		{
			get { return sId;}
			set { sId = value;}
		}

		public string IdMaster
		{
			get { return sIdMaster;}
			set { sIdMaster = value;}
		}

		public string VersionMaster
		{
			get { return sVersionMaster;}
			set { sVersionMaster = value;}
		}

		public string LastDocument
		{
			get { return sLastDocument;}
			set { sLastDocument = value;}
		}

		public Scenario.WorkflowEngine.DataModel.WorkflowRelevantData.XMLAttributes XMLAttributes
		{
			get {return wfAttributes;}
			set {wfAttributes=value;}
		}
	
		override public void Init()
		{
			base.Init();

			sId = string.Empty;
			sIdMaster = string.Empty;
			sVersionMaster = string.Empty;
			sLastDocument = string.Empty;

			wfAttributes = new Scenario.WorkflowEngine.DataModel.WorkflowRelevantData.XMLAttributes();
			xmlDocumentVersions = new XMLDocumentVersions();
		}

		override public void Parse(System.Xml.XmlNode node2Parse)
		{
			//only Parse because Init methods is invocated from base.Parse() 
			base.Parse (node2Parse);

			System.Xml.XmlNode node;

			for ( int i=0; i<node2Parse.Attributes.Count;i++)
			{
				node = node2Parse.Attributes[i];

				switch ( node.Name )
				{
					case XMLDocumentInstance.ID_ATTRIBUTE:
						sId = node.InnerText;
						break;

					case XMLDocumentInstance.IDMASTER_ATTRIBUTE:
						sIdMaster = node.InnerText;
						break;

					case XMLDocumentInstance.VERSIONMASTER_ATTRIBUTE:
						sVersionMaster = node.InnerText;
						break;

					case XMLDocumentInstance.LASTDOCUMENT_ATTRIBUTE:
						sLastDocument = node.InnerText;
						break;
				}
			}

			for ( int i=0; i<node2Parse.ChildNodes.Count;i++)
			{
				node = node2Parse.ChildNodes[i];

				switch ( node.Name )
				{
					case XMLDocumentVersions.MAIN_NODE:
						xmlDocumentVersions.Parse(node);
						break;

					case Scenario.WorkflowEngine.DataModel.WorkflowRelevantData.XMLAttributes.MAIN_NODE:
						this.wfAttributes.Parse(node);
						break;

				}
			}
		}

		override public bOSdotNET.XML.XMLNode GetXMLNode()
		{
			bOSdotNET.XML.XMLNode xmlNode = base.GetXMLNode();

			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLDocumentInstance.ID_ATTRIBUTE, sId));
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLDocumentInstance.IDMASTER_ATTRIBUTE, sIdMaster));
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLDocumentInstance.VERSIONMASTER_ATTRIBUTE, sVersionMaster));
			xmlNode.AddAttribute ( new bOSdotNET.XML.XMLAttribute ( XMLDocumentInstance.LASTDOCUMENT_ATTRIBUTE, sLastDocument));
			xmlNode.AddNode( wfAttributes.GetXMLNode());
			xmlNode.AddNode ( xmlDocumentVersions.GetXMLNode ());

			return xmlNode;
		}
		//modified by e.leano for dm step 2
		public void ReadFromDataBase( MySqlDataReader reader)
		{
			while (reader.Read() ) 
			{
				this.Id = reader.GetValue(0).ToString();
				this.Type=reader.GetValue(1).ToString();
				this.Name=reader.GetValue(2).ToString();
				this.CreationPolicy=reader.GetValue(3).ToString();
				this.ManageVersion=reader.GetValue(4).ToString();
				this.IdMaster=reader.GetValue(5).ToString();
				this.VersionMaster=reader.GetValue(6).ToString();
				this.LastDocument=reader.GetValue(7).ToString();
				this.XMLDocumentMigrates.Parse(reader.GetValue(8).ToString());
				this.XMLGrants.Parse(reader.GetValue(9).ToString());
				//this.AddAttribute ( "DocumentStorage", reader.GetString (10));
			}
		}
	}
}
